extends TileMap

#噪音生成
@export var dirt_clay_noise_text : NoiseTexture2D
@export var coal_ore_noise_text : NoiseTexture2D
@export var iron_ore_noise_text : NoiseTexture2D
@export var copper_ore_noise_text : NoiseTexture2D

var map_data : Dictionary = {}




var dirt_clay_noise : Noise
var coal_ore_noise : Noise
var iron_ore_noise : Noise
var copper_ore_noise : Noise



#水层大小
var water_width : int = 70
var water_height : int = 70


#陆地大小
var stone_width : int = 64
var stone_height : int = 64


#地图层设定
var water_layer = 0
var stone_layer = 1
var resource_layers = 2


#图集ID
var water_id = 0
var stone_id = 1
var dirt_id = 2
var clay_id = 3
var coal_ore_id = 4
var iron_ore_id = 5
var copper_ore_id = 6


func _ready(): 
	dirt_clay_noise = dirt_clay_noise_text.noise
	coal_ore_noise = coal_ore_noise_text.noise
	iron_ore_noise = iron_ore_noise_text.noise
	copper_ore_noise = copper_ore_noise_text.noise
	
	generate_background(water_width, water_height, water_layer, water_id)
	generate_background(stone_width, stone_height, stone_layer, stone_id, 3, 3)
	
	generate_dirt_clay(-0.5, -0.4, 3, 3)
	
	generate_ore("coal_ore_noise", coal_ore_noise, coal_ore_id, -0.4, 3, 3)
	generate_ore("iron_ore_noise", iron_ore_noise, iron_ore_id, -0.35, 3, 3)
	generate_ore("copper_ore_noise", copper_ore_noise, copper_ore_id, -0.33, 3, 3)
	
	#修改噪音数据用于模拟矿石资源个数
	for key in map_data:
		if (map_data[key] != 0) && (map_data[key] != 1):
			map_data[key] = int(1/abs(map_data[key])*100)
	print(map_data)
	
	
	

#加载背景层
func generate_background(width, height, layer, id, offset_x : int = 0, offset_y : int= 0):
	for x in range(offset_x, width+offset_x):
		for y in range(offset_y, height+offset_y):
			set_cell(layer,Vector2(x, y), id, Vector2i(0, 0))
			map_data[Vector2(x, y)] = id
			
		
#加载矿石层			
func generate_ore(ore_name, ore_noise, atlas_id, generate_data, offset_x = 0, offset_y = 0):
	
	var noise_val_arr = []	
	
	for x in range(offset_x, stone_width+offset_x):
		for y in range(offset_y, stone_height+offset_y): 
			var ore_noise_val :float = ore_noise.get_noise_2d(x, y)
			
			noise_val_arr.append(ore_noise_val)
			
			if ore_noise_val >= generate_data:
				set_cell(resource_layers, Vector2(x, y), atlas_id, Vector2i(0, 0))
				map_data[Vector2(x, y)] = ore_noise_val

	print(ore_name, "_min: ", noise_val_arr.min())		
	print(ore_name, "_max: ", noise_val_arr.max())			

#加载土地层
func generate_dirt_clay(dirt_data, clay_data, offset_x = 0, offset_y = 0):
	
	var noise_val_arr = []
	
	for x in range(offset_x, stone_width+offset_x):
		for y in range(offset_y, stone_height+offset_y):
			var dirt_clay_noise_val :float = dirt_clay_noise.get_noise_2d(x, y)
			
			noise_val_arr.append(dirt_clay_noise_val)
			
			if dirt_clay_noise_val >= dirt_data:
				set_cell(resource_layers, Vector2(x, y), dirt_id, Vector2i(0, 0))
				map_data[Vector2(x, y)] = dirt_clay_noise_val
				#粘土图块生成在泥土上
				if dirt_clay_noise_val >= clay_data:
					set_cell(resource_layers, Vector2(x, y), clay_id, Vector2i(0, 0))
					map_data[Vector2(x, y)] = dirt_clay_noise_val
					
	print("dirt_clay_noise_min: ", noise_val_arr.min())
	print("dirt_clay_noise_max: ", noise_val_arr.max())				





#待定函数
#func generate_terrain(name : String, atlas_id : int, length : int, width : int, layer : int, noise : Noise, seed : int, generate_height : float,vector2i = Vector2i(0, 0), offset_x = 0, offset_y = 0):
	#var noise_val_arr = []	
	#noise.seed = seed
	#for x in range(offset_x, length+offset_x):
		#for y in range(offset_y, width+offset_y): 
			#
			#var noise_val :float = noise.get_noise_2d(x, y)
			#
			#noise_val_arr.append(noise_val)
			#
			#if noise_val >= generate_height:
				#set_cell(layer, Vector2(x, y), atlas_id, vector2i)
				#
	#print(name, "min: ", noise_val_arr.min())		
	#print(name, "max: ", noise_val_arr.max())			




